package com.jtjrenren.android.taxi.passenger.core.user.login;

import android.os.CountDownTimer;

import com.jtjrenren.android.taxi.passenger.bean.User;
import com.jtjrenren.android.taxi.passenger.bean.api.ApiResultCommonStr;
import com.jtjrenren.android.taxi.passenger.manger.AppManger;

import com.jtjrenren.android.taxi.passenger.manger.api.ApiWorks;
import com.jtjrenren.android.taxi.passenger.utils.HttpParseUtils;
import com.library.mvp.presenter.BasePresenter;
import com.library.utils.StringUtils;

/**
 * Created by Administrator on 2016/9/12.
 */

public class LoginPresenterImpl extends BasePresenter<LoginView, LoginInteractor> implements LoginPresenter {
    private int currentType = 0;
    private String phone;

    CountDownTimer countDownTimer = new CountDownTimer(30 * 1000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            view.refreshYzmDjs((int) millisUntilFinished / 1000);
        }

        @Override
        public void onFinish() {
            view.getYzmFinish();
        }
    };

    public LoginPresenterImpl(LoginView view) {
        super(view);
    }

    @Override
    public void init() {
    }

    @Override
    public void destory() {

        if (countDownTimer != null)
            countDownTimer.cancel();
        countDownTimer.cancel();
    }

    @Override
    public void changeLoginType(int type) {
        currentType = type;
        if (type == 0) {
            view.showPwdLogin();
        } else if (type == 1) {
            view.showYzmLogin();
        }
    }

    @Override
    public void beforeSendCode(final String phone) {
        if (!StringUtils.isEmpty(phone)) {
            this.phone = phone;
            interactor.checkPhone(phone, new ApiWorks.ResponseListener<ApiResultCommonStr>() {
                @Override
                public void onResponse(ApiResultCommonStr apiResultCommonStr) {

                    if (apiResultCommonStr != null && apiResultCommonStr.getCode() == 1)
                        view.showTip("手机未注册");
                    else {
                        interactor.getYzm(phone, new ApiWorks.ResponseListener<String>() {
                            @Override
                            public void onResponse(String s) {

                            }
                        });
                        startYzmDjs();
                    }
                }
            });
        }
    }


    @Override
    public void login(String phone, String pwd, String code) {
        this.phone = phone;
        if (currentType == 0) {
            interactor.loginByPwd(phone, pwd, new ApiWorks.ResponseListener<ApiResultCommonStr>() {
                @Override
                public void onResponse(ApiResultCommonStr apiResultCommonStr) {
                    User user = null;
                    if (apiResultCommonStr != null && apiResultCommonStr.getCode() == 1)
                        user = loginResultParse(apiResultCommonStr.getData());
                    handLoginResult(user);
                }
            });
        } else if (currentType == 1)
            interactor.loginByCode(phone, code, new ApiWorks.ResponseListener<ApiResultCommonStr>() {
                @Override
                public void onResponse(ApiResultCommonStr apiResultCommonStr) {
                    User user = null;
                    if (apiResultCommonStr != null && apiResultCommonStr.getCode() == 1)
                        user = loginResultParse(apiResultCommonStr.getData());
                    handLoginResult(user);
                }
            });
    }

    @Override
    public void startYzmDjs() {
        view.startYzm();
        countDownTimer.start();
    }


    private void handLoginResult(User userr) {
        if (userr != null) {
            view.showTip("登录成功");
            User user = userr;
            user.setPhone(phone);
            AppManger.getInstance().loginSuccess(user);
            view.finishOk(true);
        } else {
            view.showTip("登录失败");
        }
    }

    @Override
    public LoginInteractor createInteractor() {
        return new LoginInteractorImpl();
    }

    private User loginResultParse(String jsonStr) {
        User user = null;
        if (!StringUtils.isEmpty(jsonStr)) {
            String[] resuts = HttpParseUtils.parseUserCommonResult(jsonStr);
            if (resuts.length == 3) {
                user = new User();
                user.setWorkNo(resuts[0]);
            }
        }
        return user;
    }
}
